package ysq.xyg.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysRoleMenu;
import ysq.xyg.sys.model.vo.SysRoleMenuVo;
import ysq.xyg.sys.service.SysRoleMenuService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统管理-角色菜单controller层
 *
 * @author shengQiang yu
 * @date 2019-4-8 8:55:21
 */
@RestController
public class SysRoleMenuController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SysRoleMenuController.class);
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    private SysRoleMenu sysRoleMenu;

    /**
     * 分页查询
     *
     * @param sysRoleMenuVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    @BaseLog(module = "系统管理",className = "角色菜单",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysRoleMenu/page",method = RequestMethod.POST)
    public ReturnEntity page(SysRoleMenuVo sysRoleMenuVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = sysRoleMenuService.page(sysRoleMenuVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    @RequestMapping(value = "/sysRoleMenu/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/sys/sysRoleMenuUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            sysRoleMenu = sysRoleMenuService.selectByPrimaryKey(id);
            view.addObject("sysRoleMenu", sysRoleMenu);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    @BaseLog(module = "系统管理",className = "角色菜单",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysRoleMenu/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return sysRoleMenuService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 菜单集合用于导航
     *
     * @param roleId 角色id
     * @param menuId 菜单Id
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    @RequestMapping(value = "/sysRoleMenu/menuList")
    public JSONObject menuList(String roleId, String menuId){
        //菜单查询
        SysRoleMenuVo sysRoleMenuVo=new SysRoleMenuVo();
        sysRoleMenuVo.setRoleId(roleId);
        sysRoleMenuVo.setParentId(menuId);
        sysRoleMenuVo.setMenuType(BaseConstant.SYS_MENU_TYPE_MENU);
        List<SysRoleMenu> sysRoleMenuList=sysRoleMenuService.listLeft(sysRoleMenuVo);
        JSONObject jsonObject=new JSONObject();
        JSONArray rows = new JSONArray();
        for (SysRoleMenu sysRoleMenu:sysRoleMenuList){
            JSONObject row=new JSONObject();
            row.put("title",sysRoleMenu.getMenuName());
            row.put("icon",sysRoleMenu.getMenuIcon());
            row.put("href",sysRoleMenu.getMenuUrl());
            row.put("spread",false);
            rows.add(row);
        }
        jsonObject.put("rows",rows);
        return jsonObject;
    }
    /**
     * 添加或修改
     *
     * @param sysRoleMenuVo Vo对象
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    @BaseLog(module = "系统管理",className = "角色菜单",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysRoleMenu/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,SysRoleMenuVo sysRoleMenuVo){
        return sysRoleMenuService.addOrUpdate(sysRoleMenuVo,acceptCurrentUser(request));
    }
}